/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.vod.gauss.matrice;

import java.util.List;
import fr.vod.gauss.utils.Convert;

/**
 *
 * @author drazanakoto
 */
public class MatriceImpl implements Matrice {

    private final Equation[] equations;
    private int nb;

    /**
     *
     * @param mx
     */
    public MatriceImpl(int mx) {
        equations = new Equation[mx];
        nb = 0;
    }

    /**
     *
     * @return
     */
    @Override
    public int getLength() {
        return nb;
    }

    /**
     *
     * @param index
     * @return
     */
    @Override
    public Equation get(int index) {
        if (index >= equations.length) {
            return null;
        }
        return equations[index];
    }

    /**
     *
     * @param index
     */
    @Override
    public void remove(int index) {
        if (index < equations.length) {
            equations[index] = null;
            reorganiser(index);
            nb--;
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (Equation e : equations) {
            if (e != null) {
                sb.append(e.toString()).append("\n");
            }
        }
        return sb.toString();
    }

    /**
     *
     * @param src
     * @param dest
     */
    @Override
    public void move(int src, int dest) {
        if (dest < equations.length && dest >= 0 && src < equations.length && src >= 0) {
            Equation tmp = equations[src];
            equations[src] = equations[dest];
            equations[dest] = tmp;
        }
    }

    /**
     *
     * @param e
     */
    @Override
    public void addEquation(Equation e) {
        int tmp;
        if (e != null && e.getSize() == equations.length) {
            tmp = e.isSolve();
            if (tmp > -1 && equations[tmp] == null) {
                equations[tmp] = e;
                nb++;
            } else if (tmp > -1 && equations[tmp].isSolve() > 0) {
                equations[tmp] = e;
            } else if (nb < equations.length) {
                equations[nb] = e;
                nb++;
            }
        }
    }

    @Override
    public byte[] rebuildFile() {
        byte data[] = new byte[equations.length];
        int i = 0;
        for (Equation e : equations) {
            data[i] = Convert.uint8ToByte(e.getResult());
            i++;
        }
        return data;
    }

    @Override
    public void addAll(List<Equation> lst) {
        if (lst != null) {
            for (Equation e : lst) {
                addEquation(e);
            }
        }
    }

    /**
     *
     * @param indice
     */
    protected void reorganiser(int indice) {
        for (int i = indice; i < equations.length; i++) {
            equations[i] = i < equations.length - 1 ? equations[i + 1] : null;
        }
    }

	@Override
	public boolean needToSolve() {
		for(Equation e:equations){
			if(e.isSolve() < 0){
				return true;
			}
		}
		return false;
	}
}
